package com.accp.ssmoa.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.accp.ssmoa.pojo.Department;
import com.accp.ssmoa.service.DepartmentService;
import com.alibaba.fastjson.JSONObject;

@Controller
@Scope("prototype")
@RequestMapping("/admin/department")
public class DepartmentController {

	@Resource(name = "departmentServiceImpl")
	private DepartmentService departmentService;

	// 显示部门信息树形图
	@RequestMapping("/getDepartmentTree")
	@ResponseBody
	public List<Object> getDepartmentTree() {

		List<Object> departmentTree = departmentService.getDepartmentsTree();

		return departmentTree;
	}

	// 分页显示部门信息
	@RequestMapping(value = "/listData", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> listData(@RequestBody JSONObject jsonObj) {

		// 取出前台传过来的封装在json格式里面的数据
		Integer pageSize = jsonObj.getIntValue("pageSize"); // 每页显示数据条数
		Integer startRow = jsonObj.getIntValue("startRow"); // 当前页码


		Integer parentmentId=null;

		if(!jsonObj.getString("parentmentId").equalsIgnoreCase("")) {
			parentmentId=jsonObj.getIntValue("parentmentId");
		}

		int totalsize = departmentService.getTotalsize(parentmentId);// 总数据数
		Map<String, Object> map = new HashMap<String, Object>();

		List<Department> departments = departmentService.getPageDepartment(startRow, pageSize,parentmentId);

		// total表示数据总数，rows表示需要显示的数据。必须按照这种格式返回才行。
		map.put("total", totalsize);
		map.put("rows", departments);

		return map;
	}

	// 获取弹出选择父级机构树形图
	@RequestMapping("/getOneDepartmentTree")
	public String getOneDepartmentTree() {

		return ("common/pointLion/selectOneOrg/selectOneOrg");
	}

	// 实现添加和修改
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Map<String, Boolean> saveOrUpdate(@ModelAttribute("Department") Department department,
			HttpServletRequest request) {

		Map<String, Boolean> result = new HashMap<String, Boolean>();

		int a;// 添加或保存持久化操作后的返回值
		String opreate = (String) request.getParameter("opreate");// 判断操作

		// 判断是进行修改还是添加
		if (opreate.equalsIgnoreCase("修改机构")) {
			a = departmentService.update(department);
		} else {
			a = departmentService.save(department);
		}

		if (a == 1) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}

		return result;

	}


	
	
	
	
	/* 需要进行权限控制的操作，增删改以及授权 */

	// 初始化部门信息页面
	@RequestMapping("/getListPage")
	public String getDepartmentList(HttpServletRequest request) {

		// 存储面包屑导航的值进request作用域
		request.setAttribute("pageTitle", "组织机构");

		request.setAttribute("url", "/admin/department/getListPage");

		request.setAttribute("nowBread", "管理");

		return "org/list";
	}

	// 实现删除
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam(value = "idsArr[]") String[] idsAttr) {

		Map<String, Object> result = new HashMap<String, Object>();
		List<Integer> ids = new ArrayList<Integer>();

		for (String idString : idsAttr) {

			ids.add(Integer.parseInt(idString));
		}

		int a = 0;

		a = departmentService.delete(ids);

		if (a > 0) {
			result.put("success", true);
			result.put("message", "删除成功");
		} else {
			result.put("success", false);
			result.put("message", "删除失败");
		}

		return result;

	}

	// 提供添加、添加下级机构信息页面
	@RequestMapping("/save")
	public String save(HttpServletRequest request) {

		// 存储面包屑导航的值进request作用域
		request.setAttribute("pageTitle", "组织机构");
		request.setAttribute("url", "/admin/department/getListPage");

		// 根据传入的参数判断是进行哪个操作
		if (request.getParameter("parentid") != null) {

			request.setAttribute("nowBread", "添加下级机构");

			Integer id = Integer.parseInt(request.getParameter("parentid"));

			// 根据id查询机构信息
			Department department = departmentService.getDepartmentById(id);

			Department department2 = new Department();

			department2.setParentdepartmentid(department.getId());
			department2.setParent_name(department.getName());

			request.setAttribute("department", department2);

		} else {

			request.setAttribute("nowBread", "添加新机构");
		}

		return "org/edit";
	}

	// 提供修改机构信息页面
	@RequestMapping("/update")
	public String update(HttpServletRequest request) {

		// 存储面包屑导航的值进request作用域
		request.setAttribute("pageTitle", "组织机构");
		request.setAttribute("url", "/admin/department/getListPage");

		request.setAttribute("nowBread", "修改机构");

		Integer id = Integer.parseInt(request.getParameter("id"));

		// 根据id查询机构信息
		Department department = departmentService.getDepartmentById(id);

		request.setAttribute("department", department);

		return "org/edit";
	}

}
